package com.ruoyi.operation.order.total.service.impl;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.operation.order.depot.domain.Depot;
import com.ruoyi.operation.order.total.domain.Total;
import com.ruoyi.operation.order.total.mapper.ITotalMapper;
import com.ruoyi.operation.order.total.service.ITotalService;
import com.ruoyi.operation.order.util.ExportExcelUtil;
import com.ruoyi.project.system.role.mapper.RoleMapper;
import com.ruoyi.project.system.user.mapper.UserMapper;
//import com.sun.corba.se.impl.oa.toa.TOA;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @Author king
 * @create 2019/4/16 20:43
 */
@Service
public class TotalServiceImpl implements ITotalService {


    @Autowired
    private ITotalMapper iTotalMapper;
    @Autowired
    private RoleMapper iRoleMapper;

    @Autowired
    private UserMapper userMapper;


    /**
     * 查询所有的订单信息
     * @param total
     * @return
     */
    @Override
    public List<Total> allTotal(Total total) {
        return iTotalMapper.allTotal(total);
    }

    @Override
    public List<Total> total(Total total) {
        return iTotalMapper.total(total);
    }

    /**
     * 导出
     * @param total
     * @param request
     * @param response
     * @return
     */
    @Override
    public void exportTotal(Total total, HttpServletRequest request, HttpServletResponse response) throws  Exception {
        // 获取当前登陆人的id
        String getUserId = ShiroUtils.getLoginId();
        // 更具当前登陆人获取roleId
        List<String> getRoleId = iRoleMapper.getRoleId(getUserId);

        total.setUserName(getUserId);
        List<Total> TotalList = this.total(total);
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建导的Excel表的名字
        //sheet名
        HSSFSheet sheet = workbook.createSheet("订单统计表");
        // 设置导出文件的名字和导出的格式
        String fileName = "订单统计表"+".xls";
        String agent = request.getHeader("USER-AGENT");
        if(agent != null && agent.toLowerCase().indexOf("firefox") > 0)
        {
            fileName = "=?UTF-8?B?" + (new String(Base64Utils.encodeToString(fileName.getBytes("UTF-8")))) + "?=";
        } else {
            fileName =  java.net.URLEncoder.encode(fileName, "UTF-8");
        }
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        // 新增数据行，设置单元格数据
        int rowNum  = 1;
        // 定义导出Excel的表格
        String[] title = {"录单员","平台","店铺","总订单数","总订单金额","总佣金金额","好评数","好评金额"};
        String[] colList = {"userName","deptName","shopName","orderNumCount","orderPriceCount","commissionPriceCount","goodRecordCount","goodPriceCount"};
        HSSFRow row = sheet.createRow(0);
        // 在Excel表中添加表头
        for (int i = 0;i<title.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(title[i]);
            cell.setCellValue(text);
        }
        // 在表中存放查询到的数据到对应的列
        for (Total c: TotalList){
            HSSFRow row1 = sheet.createRow(rowNum);
            int j = 0;
            for(String s:colList){
                try {
                    row1.createCell(j).setCellValue((String)getGetMethod(c, s));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                j++;
            }
            rowNum++;
        }
        ExportExcelUtil.outPut(response,workbook,fileName);
    }

    /**
     * 根据属性，获取get方法
     * @param ob 对象
     * @param name 属性名
     * @return
     * @throws Exception
     */
    private Object getGetMethod(Object ob , String name)throws Exception{
        Method[] m = ob.getClass().getMethods();
        for(int i = 0;i < m.length;i++){
            if(("get"+name).toLowerCase().equals(m[i].getName().toLowerCase())){
                return m[i].invoke(ob);
            }
        }
        return null;
    }

}
